import { _decorator, Label, Node, tween, UIOpacity, v3 } from "cc";
import { BaseUI } from "../../../../FrameWork/Module/Component/BaseUI";
import { ITip } from "../../../../FrameWork/Common/BaseInterface";
const { ccclass, property } = _decorator;

/**
 * 通用提示
 */
@ccclass("TipUI")
export class TipUI extends BaseUI {
    @property({ type: Node, displayName: "提示内容" })
    nodeTip: Node = null;

    protected data: ITip = null;
    protected init(): void {
        super.init();
    }

    protected onShow(): void {
        super.onShow();
        this._setTipContent();
        this._playTipAni();
    }

    /**
     * 设置提示内容
     */
    private _setTipContent(): void {
        const lblTip = this.nodeTip.getChildByName("lblTip").getComponent(Label);
        lblTip.string = this.data.tip;
    }
    /**
     * 播放tip动画
     * 执明提供
     */
    private _playTipAni(): void {
        const op = this.nodeTip.getComponent(UIOpacity) || this.nodeTip.addComponent(UIOpacity);
        op.opacity = 0;
        this.nodeTip.setPosition(v3(0, 0));
        tween(op).stop();
        tween(this.nodeTip).stop();
        tween(op)
            .to(0.5, { opacity: 255 }, { easing: "cubicOut" })
            .delay(1)
            .call(() => {
                tween(this.nodeTip)
                    .by(0.5, { position: v3(0, 125) })
                    .call(() => {
                        this.nodeTip.setPosition(v3(0, 0));
                    })
                    .start();

                tween(op)
                    .to(0.5, { opacity: 0 })
                    .call(() => {
                        this.closeUI();
                    })
                    .start();
            })
            .start();
    }
}
